<?
	/* Se asegura que el archivo es incluido por el index */
		defined( 'LOAD_MODULE_ACTION' ) or die( 'No esta permitido el acceso directo a ' . __FILE__ );
	/* Cuando se hace el include del archivo automaticamente crea un objeto y lo guarda en una variable temporal */
		$this->oTmpObject = new Tarea_frm( $this );

	class Tarea_frm extends Frm
	{
	function Tarea_frm( &$oModule )
		{
			$this->Frm( $oModule );
			//Crea los Objetos de los campos de edicion
			$this->aFields['ncVisibilidad'] 				= new InputList ( "Visibilidad", "ncVisibilidad" );
			$this->aFields['ncTarea'] 						= new Input( "Codigo", "ncTarea", '', false, true );
			$this->aFields['ncTareaPadre']			= new InputList( "Subtarea de", "ncTareaPadre" );
			$this->aFields['fiCosto']						= new InputNumber( "Costo", "fiCosto" );
			$this->aFields['nqTiempo']					= new InputNumber( "Tiempo", "nqTiempo" );
			$this->aFields['aPersona'] 					= new InputMultiple( "Responsables",  "aPersona", getParam('ncPersona') );
			$this->aFields['aPersona']->setInput( new InputLookUp( "" ) );
			$this->aFields['aPersona']->setValueSeparator( ";" );
			$this->aFields['ncPrioridad'] 				= new InputList ( "Prioridad", "ncPrioridad" );
			$this->aFields['ncCategoriaTarea'] 		= new InputList ( "Categoria", "ncCategoriaTarea" );
			$this->aFields['cdTarea'] 						= new InputText ( "Nombre", "cdTarea" );
			$this->aFields['cDescripcion'] 				= new InputText ( "Descripcion" ,"cDescripcion" , "", true, true);
			$this->aFields['Periodo'] 						= new InputArray( "Periodo");
			$this->aFields['Periodo']->addInput( new InputDate( "Vto", "dVencimiento" ) );
			$this->aFields['Periodo']->addInput( new InputDatePeriod( "Periodo", "dDesde", "dHasta" ) );
			$this->aFields['ncProyecto'] 			= new InputList ( "Proyecto", "ncProyecto" );
	}
	function setValue( $ncTarea )
		{
			$this->aFields['ncTarea']->setValue( $ncTarea );

			$cSql = "SELECT *
								FROM atTarea
								WHERE ncTarea = $ncTarea";
			$aRow = $this->oDatabase->getArray($cSql);
			$this->setFieldsArray ( $aRow );
			$this->aFields['Periodo']->aInput['dVencimiento']->setValue( $aRow['dVencimiento'] );
			$this->aFields['Periodo']->aInput['dDesde']->oInputDesde->setValue( $aRow['dDesde'] );
			$this->aFields['Periodo']->aInput['dDesde']->oInputHasta->setValue( $aRow['dHasta'] );
			// Si Tiene subtareas => le pone el Tiempo y Costo de las subtareas
				list( $fiCostoSubTareas, $nqTiempoSubTareas, $nqSubTareas )= $this->oDatabase->getArray("SELECT SUM(fiCosto) , SUM(nqTiempo) , COUNT(*) FROM atTarea WHERE ncTareaPadre =  $ncTarea"  ) ;
				if ( $nqSubTareas > 0 )
				{
					$this->aFields['fiCosto']->setValue($fiCostoSubTareas);
					$this->aFields['nqTiempo']->setValue($nqTiempoSubTareas);
					$this->aFields['fiCosto']->setDisabled();
					$this->aFields['nqTiempo']->setDisabled();
				}

			return $aRow;
		}
	function make ( $nOption = '')
		{
			$this->mergeOptions( $cOptions );
			// Si Recibe la Pk => busca los datos
				if ( $this->isRetrievable() &&  $this->aFields['ncTarea']->getValue() )
				{
					$aRow = $this->setValue( $this->aFields['ncTarea']->getValue() );
					// Verifica que tenga Permisos
					$ncMiGrupo = obtenerMiGrupo();
					$bPermisoEscritura = ($aRow['ncVisibilidad'] == VISIBILIDAD_PUBLICO || $aRow['ncGrupo'] == $ncMiGrupo);
					if ( !$bPermisoEscritura )
					{
						$bPermisoLectura = ($aRow['ncVisibilidad'] != VISIBILIDAD_PRIVADO || $aRow['ncGrupo'] == $ncMiGrupo);
						if ( $bPermisoLectura )
						{
							$oDet = $this->oModule->loadClass( 'tarea_lst' );
							$oDet->makeDet();
							return true;
						} else
						{
							addMessage( "No tiene permisos" );
							return false;
						}
					}
				}
			return parent::make( $nOption );
		}
	function initializeForm()
		{
			// Configura el Titulo
				$this->setLabel( "Tarea" );
				if ( $this->aFields['ncTareaPadre']->getValue()  )
				{
					$cSql = "SELECT cdTarea, ncProyecto FROM atTarea WHERE ncTarea =  " . $this->aFields['ncTareaPadre']->getValue()  ;
					list($cdTarea, $ncProyecto) = $this->oDatabase->getArray($cSql);
					$this->setLabel( "Subtarea de " . $cdTarea  );
					$this->aFields['ncTareaPadre']->setHidden() ;
					if ( $ncProyecto )	$this->aFields['ncProyecto']->setValue( $ncProyecto) ;
					else 
					{
						$cSql = "SELECT TRe.ncPersona, cNombre, cApellido FROM atTareaResponsable as TRe JOIN atPersona as Per ON Per.ncPersona = TRe.ncPersona WHERE ncTarea = " . $this->aFields['ncTareaPadre']->getValue() ;
						$aPersonas = $this->oDatabase->getMatrix($cSql);
						foreach( $aPersonas as $aPersona )
						{
							$this->aFields['aPersona']->addOption( $aPersona['ncPersona'] ,  $aPersona['cNombre'] . ' ' . $aPersona['cApellido'] );
						}
					}
				}
				// Si es un proyecto => cambia el control InputMultiple por InputCheck
				if ($this->aFields['ncProyecto']->getValue()  ) 	$this->aFields['aPersona'] 					= new InputCheck( "aPersona"  );
			return true;
		}
	function initializeField( $cField , &$oField )
		{
			switch ( $cField )
			{
				case 'ncVisibilidad':
					$cSql = "SELECT ncVisibilidad, cdVisibilidad FROM acVisibilidad";
					$oField->setDefault( VISIBILIDAD_PUBLICO );
					$oField->setMatrix( $this->oDatabase->getMatrix($cSql) );
				break;
				case 'ncTareaPadre':
					$oField->setWidth(150);
					$cSql = "SELECT Tar.ncTarea, cdTarea FROM atTarea as Tar ";
					$cSql .= " WHERE dCompleto is NULL ";
					if  ( $this->aFields['ncProyecto']->getValue() ) $cSql .=  " AND ncProyecto = " . $this->aFields['ncProyecto']->getValue() ;
					if ( $this->aFields['ncTarea']->getValue()  )
					{
						$cSqlBase = "SELECT ncTarea FROM atTarea WHERE dCompleto is NULL ";
						$cSubTareas = $this->aFields['ncTarea']->getValue();
						$aSubTareas = $this->oDatabase->getArray( $cSqlBase  . " AND ncTareaPadre IN ( $cSubTareas ) " );
						while ( is_array ( $aSubTareas )  && count($aSubTareas) > 0  )
						{
							$cSubTareasActual  =  implode( "," , $aSubTareas ) ;
							$cSubTareas .= ',' . $cSubTareasActual ;
							$aSubTareas = $this->oDatabase->getArray( $cSqlBase . " AND ncTareaPadre IN ( $cSubTareasActual) " );
						}
						$cSql .=  " AND ncTarea not in (  $cSubTareas  ) " ;
					}
					$oField->setMatrix( $this->oDatabase->getMatrix($cSql)  );
				break;
				case 'Periodo':
					$oField->aInput['dDesde']->oInputDesde->setDefault( Date("Y-m-d") );
				break;
				case 'aPersona':
					if ($this->aFields['ncProyecto']->getValue()  )
					{
						if ($this->aFields['ncTarea']->getValue()  )
						{
							$cSql = "SELECT PRe.ncPersona, cApellido , TRe.ncPersona
												FROM atProyectoPersona as PRe
												JOIN atPersona as Per
													ON Per.ncPersona = PRe.ncPersona
												LEFT JOIN atTareaResponsable as TRe
													ON TRe.ncPersona = PRe.ncPersona
													AND TRe.ncTarea = " . $this->aFields['ncTarea']->getValue()  . "
										WHERE PRe.ncProyecto= " . $this->aFields['ncProyecto']->getValue() ;
						} else
						{
							// Si es una subtarea dentro de un proyecto
							if ( $this->aFields['ncTareaPadre']->getValue()  )
								$cSql = "SELECT PRe.ncPersona, cApellido, TRe.ncPersona 
												FROM atProyectoPersona as PRe 
												LEFT JOIN atTareaResponsable TRe
													ON TRe.ncPersona = PRe.ncPersona
													AND ncTarea =  " . $this->aFields['ncTareaPadre']->getValue() . "
												JOIN atPersona as Per ON Per.ncPersona = PRe.ncPersona 
												WHERE ncProyecto= " . $this->aFields['ncProyecto']->getValue() ;
							else
								$cSql = "SELECT PRe.ncPersona, cApellido FROM atProyectoPersona as PRe JOIN atPersona as Per ON Per.ncPersona = PRe.ncPersona WHERE ncProyecto= " . $this->aFields['ncProyecto']->getValue() ;
						}
						$oField->setMatrix( $this->oDatabase->getMatrix( $cSql ) );
						$oField->setMultiple( true );
					} else
					{
						$oField->oInput->setField( "aPersona_txt" );
						$oField->oInput->setFieldActions( 'ncPersona');
						initializePersona ( $oField->oInput );
						$oField->setWidth(330);
						$oField->setForm( $this );
						// Carga los Responsables
						if ($this->aFields['ncTarea']->getValue()  )
						{
							$cSql = "SELECT TRe.ncPersona, cNombre, cApellido FROM atTareaResponsable as TRe JOIN atPersona as Per ON Per.ncPersona = TRe.ncPersona WHERE ncTarea = $ncTarea";
							$aPersonas = $this->oDatabase->getMatrix($cSql);
							foreach( $aPersonas as $aPersona )
							{
								$this->aFields['aPersona']->addOption( $aPersona['ncPersona'] ,  $aPersona['cNombre'] . ' ' . $aPersona['cApellido'] );
							}
						}
					}
				break;
				case 'ncPrioridad':
					$cSql = "SELECT ncPrioridad, cdPrioridad FROM acPrioridad";
					$oField->setRadio();
					$oField->setMatrix( $this->oDatabase->getMatrix( $cSql )  );
				break;
				case 'ncCategoriaTarea':
					$cSql = "SELECT ncCategoriaTarea, cdCategoriaTarea FROM atCategoriaTarea";
					$cSql .= " WHERE ncGrupo = " . obtenerMiGrupo() ;
					$oField->setMatrix( $this->oDatabase->getMatrix( $cSql )  );
					$oField->setForm( $this );
					$oField->addActionArray( 2, $this->oModule->getActionArray( AGREGAR_CATEGORIA_TAREA) , '', $oField->getField() );
					$oField->addActionArray( 2, $this->oModule->getActionArray( MODIFICAR_CATEGORIA_TAREA) , '', $oField->getField() );
				break;
				case 'cdTarea':
					$oField->setMandatory( $this->isMandatory() );
					$oField->setLength( 60);
				break;
				case 'ncProyecto':
					$oField->setMatrix( $this->oDatabase->getMatrix("SELECT ncProyecto, cdProyecto FROM atProyecto") );
				break;
			}
		}
	}
?>